# -*- coding:utf-8 -*-

from mitmproxy import ctx
import json
from pymongo import MongoClient
from datetime import datetime


class DB(object):
    def __init__(self):
        self.client = MongoClient('127.0.0.1', 27017)
        self.db = self.client["DouYin"]
        self.collection = self.db["双一流高校_用户"]
        self.collection1 = self.db['作品_新疆大学']
    def insertItem(self, data):
        self.collection.insert_one(data)
    def insertItem1(self, data):
        self.collection1.insert_one(data)

    def update_one(self, uid, desc, comment_count, digg_count, share_count, shoot_duration, create_time):
        # uu = self.db["非遗"].find_one({'uid': uid})
        self.db["双一流高校_用户"].update_many({'uid': uid},
                                  {'$set':{'desc': desc, 'comment_count': comment_count, 'digg_count': digg_count,
                                           'share_count': share_count, 'shoot_duration': shoot_duration,
                                           'create_time': create_time}})
db = DB()

# mitmdump.exe -p 8888 -s E:\Workspaces\Python\Spider\douyin\douyin_user_script.py
# 所有发出的请求数据包都会被这个方法所处理
# 所谓的处理，我们这里只是打印一下一些项；当然可以修改这些项的值直接给这些项赋值即可
def request(flow):
    # # 获取请求对象
    # request = flow.request
    # # 实例化输出类
    # info = ctx.log.info
    # # 打印请求的url
    # info(request.url)
    # # 打印请求方法
    # info(request.method)
    # # 打印host头
    # info(request.host)
    # # 打印请求端口
    # info(str(request.port))
    # # 打印所有请求头部
    # info(str(request.headers))
    # # 打印cookie头
    # info(str(request.cookies))
    pass

# 所有服务器响应的数据包都会被这个方法处理
# 所谓的处理，我们这里只是打印一下一些项
def response(flow):
    # 获取响应对象
    response = flow.response
    # # 实例化输出类
    # info = ctx.log.info
    # # 打印响应码
    # info(str(response.status_code))
    # # 打印所有头部
    # info(str(response.headers))
    # # 打印cookie头部
    # info(str(response.cookies))
    # # 打印响应报文内容
    # info(str(response.text))

    try:
        # 获取V用户信息GET
        # if 'aweme-eagle-hl.snssdk.com/aweme/v1/user/' in flow.request.url:
        if '/aweme/v1/user/profile/other' in flow.request.url:
            ctx.log.info("============获取数据=============")
            datajson = json.loads(response.text)
            dict_data = dict()
            dict_data['country'] = datajson['user']['country']  # 国家
            dict_data['province'] = datajson['user']['province']  # 省份
            dict_data['city'] = datajson['user']['city']  # 城市
            dict_data['enterprise_verify_reason'] = datajson['user']['enterprise_verify_reason']  # 认证名称
            dict_data['nickname'] = datajson['user']['nickname']  # 昵称
            dict_data['uid'] = datajson['user']['uid']  # 用户ID
            dict_data['unique_id'] = datajson['user']['unique_id']  # 抖音号
            dict_data['short_id'] = datajson['user']['short_id']  # 抖音号
            dict_data['total_favorited'] = datajson['user']['total_favorited']  # 获赞数量
            dict_data['following_count'] = datajson['user']['following_count']  # 关注数量
            dict_data['mplatform_followers_count'] = datajson['user']['mplatform_followers_count']  # 粉丝数量
            dict_data['aweme_count'] = datajson["user"]['aweme_count']  # 作品数量
            dict_data['dongtai_count'] = datajson["user"]['dongtai_count']  # 动态数量
            dict_data['favoriting_count'] = datajson["user"]['favoriting_count']  # 喜欢数量
            dict_data['signature'] = datajson["user"]['signature']  # 签名
            # dict_data['tel'] = datajson['user']['commerce_info']['offline_info_list'][0]['action'] # 联系方式
            # 联系方式
            try:
                for i in datajson['user']['commerce_info']['offline_info_list']:
                    if str(i['offline_info_type']) == "3":
                        dict_data['tel'] = i['action']
            except:
                pass
            dict_data['url'] = "https://www.douyin.com/share/user/" + dict_data['uid']  # 链接
            # db.update_one(dict_data['uid'], dict_data['total_favorited'],
            #               dict_data['following_count'], dict_data['mplatform_followers_count'])
            print(f"======{dict_data['uid']}============")
            db.insertItem(dict_data)
            # print(str(dict_data))
            ctx.log.info("============Insert=============")
    except Exception as ex:
        ctx.log.warn("============错误=============")
        ctx.log.error(ex)

    # try:
    #     # 搜索-视频（标签）
    #     if 'aweme/v1/search/item' in flow.request.url:
    #         ctx.log.info("============获取数据=============")
    #         datajson = json.loads(response.text)
    #         for d in datajson['aweme_list']:
    #             dict_data = dict()
    #             dict_data['nickname'] = d['author']['nickname']  # 用户昵称
    #             dict_data['uid'] = d['author']['uid']  # 用户id
    #             dict_data['desc'] = d['desc']  # 简介
    #             dateArray = datetime.fromtimestamp(int(d['create_time']))  # 时间
    #             dict_data['create_time'] = dateArray.strftime("%Y-%m-%d %H:%M:%S")
    #             dict_data['comment_count'] = d['statistics']['comment_count']  # 评论数量
    #             dict_data['digg_count'] = d['statistics']['digg_count']  # 点赞数量
    #             dict_data['share_count'] = d['statistics']['share_count']  # 分享数量
    #             dict_data['share_url'] = d['share_url']  # 分享链接
    #             dict_data['play_addr'] = d['video']['play_addr']['list_url'][0]  # 视频地址
    #             dict_data['owner_nickname'] = d['music']['owner_nickname']  # 视频拥有者昵称
    #             dict_data['shoot_duration'] = d['music']['shoot_duration']  # 视频时长
    #
    #             db.insertItem(dict_data)
    #         # print(str(dict_data))
    #         ctx.log.info("============Insert=============")
    # except Exception as ex:
    #     ctx.log.warn("============错误=============")
    #     ctx.log.error(ex)

    # try:
    #     # 获取视频信息-动态
    #     if '/aweme/v1/aweme/post' in flow.request.url:
    #         ctx.log.info("============获取数据=============")
    #         datajson = json.loads(response.text)
    #         for d in datajson['aweme_list']:
    #             dict_data = dict()
    #             dict_data['nickname'] = d['author']['nickname']  # 用户昵称
    #             dict_data['uid'] = d['author']['uid']  # 用户id
    #             dict_data['desc'] = d['desc']  # 简介
    #             dateArray = datetime.fromtimestamp(int(d['create_time']))  # 时间
    #             dict_data['create_time'] = dateArray.strftime("%Y-%m-%d %H:%M:%S")
    #             dict_data['comment_count'] = d['statistics']['comment_count']  # 评论数量
    #             dict_data['digg_count'] = d['statistics']['digg_count']  # 点赞数量
    #             dict_data['share_count'] = d['statistics']['share_count']  # 分享数量
    #             dict_data['share_url'] = d['share_url']  # 分享链接
    #             dict_data['play_addr'] = d['video']['play_addr']['list_url'][0]  # 视频地址
    #             dict_data['owner_nickname'] = d['music']['owner_nickname']  # 视频拥有者昵称
    #             dict_data['shoot_duration'] = d['music']['shoot_duration']  # 视频时长
    #             # db.update_one(dict_data['uid'], dict_data['desc'], dict_data['comment_count'], dict_data['digg_count'],
    #             #               dict_data['share_count'], dict_data['shoot_duration'], dict_data['create_time'])
    #             db.insertItem1(dict_data)
    #         # print(str(dict_data))
    #         ctx.log.info("============Insert=============")
    # except Exception as ex:
    #     ctx.log.warn("============错误=============")
    #     ctx.log.error(ex)

    # try:
    #     # 获取视频信息-动态
    #     if 'aweme/v1/forward/list' in flow.request.url:
    #         ctx.log.info("============获取数据=============")
    #         datajson = json.loads(response.text)
    #         for d in datajson['dongtai_list']:
    #             dict_data = dict()
    #             dict_data['nickname'] = d['aweme']['author']['nickname']  # 用户昵称
    #             dict_data['uid'] = d['aweme']['author']['uid']  # 用户id
    #             dict_data['desc'] = d['aweme']['desc']  # 简介
    #             dateArray = datetime.fromtimestamp(int(d['aweme']['create_time']))  # 时间
    #             dict_data['create_time'] = dateArray.strftime("%Y-%m-%d %H:%M:%S")
    #             dict_data['comment_count'] = d['aweme']['statistics']['comment_count']  # 评论数量
    #             dict_data['digg_count'] = d['aweme']['statistics']['digg_count']  # 点赞数量
    #             dict_data['share_count'] = d['aweme']['statistics']['share_count']  # 分享数量
    #             dict_data['share_url'] = d['aweme']['share_url']  # 分享链接
    #             dict_data['play_addr'] = d['aweme']['video']['play_addr']['list_url'][0]  # 视频地址
    #             dict_data['owner_nickname'] = d['aweme']['music']['owner_nickname']  # 视频拥有者昵称
    #             dict_data['shoot_duration'] = d['aweme']['music']['shoot_duration']  # 视频时长
    #             db.update_one(dict_data['uid'], dict_data['desc'], dict_data['comment_count'], dict_data['digg_count'],
    #                           dict_data['share_count'], dict_data['shoot_duration'], dict_data['create_time'])
    #             # db.insertItem(dict_data)
    #         # print(str(dict_data))
    #         ctx.log.info("============Insert=============")
    # except Exception as ex:
    #     ctx.log.warn("============错误=============")
    #     ctx.log.error(ex)


    # try:
    #     # 搜索-话题（标签）
    #     if 'aweme/v1/challenge/aweme' in flow.request.url:
    #         ctx.log.info("============获取数据=============")
    #         datajson = json.loads(response.text)
    #         for d in datajson['aweme_list']:
    #             dict_data = dict()
    #             dict_data['aweme_id'] = d['aweme_id']  # 视频ID
    #             dict_data['nickname'] = d['author']['nickname']  # 用户昵称
    #             dict_data['uid'] = d['author']['uid']  # 用户id
    #             dict_data['desc'] = d['desc']  # 简介
    #             dateArray = datetime.fromtimestamp(int(d['create_time']))  # 时间
    #             dict_data['create_time'] = dateArray.strftime("%Y-%m-%d %H:%M:%S")
    #             dict_data['comment_count'] = d['statistics']['comment_count']  # 评论数量
    #             dict_data['digg_count'] = d['statistics']['digg_count']  # 点赞数量
    #             dict_data['share_count'] = d['statistics']['share_count']  # 分享数量
    #             dict_data['share_url'] = d['share_url']  # 分享链接
    #             dict_data['play_addr'] = d['video']['play_addr']['list_url'][0]  # 视频地址
    #             dict_data['owner_nickname'] = d['music']['owner_nickname']  # 视频拥有者昵称
    #             dict_data['shoot_duration'] = d['music']['shoot_duration']  # 视频时长
    #
    #             db.insertItem(dict_data)
    #         # print(str(dict_data))
    #         ctx.log.info("============Insert=============")
    # except Exception as ex:
    #     ctx.log.warn("============错误=============")
    #     ctx.log.error(ex)
    #
    #
    # try:
    #     # 评论内容:
    #     if 'aweme/v2/comment/list' in flow.request.url:
    #         ctx.log.info("============获取数据=============")
    #         datajson = json.loads(response.text)
    #         for d in datajson["comments"]:
    #             data = dict()
    #             data['aweme_id'] = d['aweme_id']  # 视频ID
    #             data['cid'] = d['cid']  # 当前评论ID
    #             data["text"] = d['text']
    #             dateArray = datetime.fromtimestamp(int(d['create_time']))
    #             commentTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
    #             data['create_time'] = commentTime
    #             data['nickname'] = d['user']['nickname']
    #             data['uid'] = d['user']['uid']
    #
    #             db.insertItem1(data)
    #             ctx.log.info("============Insert=============")
    #
    # except Exception as ex:
    #     ctx.log.warn("============错误=============")
    #     ctx.log.error(ex)